package com.yueya.business.file.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.wj.kit.ImageUploadKit;
import com.yueya.business.file.model.QnImage;

public class QnImageService {
	
	
	
	public List<Record> getList(String rec_id,String file_type){
		List<Record> rellist = new ArrayList<Record>();
		String sql ="select * from qiniu_image  where rel_id =? and file_type=?  order by order_id ";
		List<Record> list = Db.find(sql,rec_id,file_type);
		  Iterator<Record> it=list.iterator();
		  while (it.hasNext()) {
		    Record qn = it.next();
			qn.set("url", ImageUploadKit.getImage(qn.getStr("id")));
			rellist.add(qn);
		}
		return rellist;
	}
	
	public List<Record> getList(String rec_id){
		List<Record> rellist = new ArrayList<Record>();
		String sql ="select * from qiniu_image  where rel_id =?   order by order_id ";
		List<Record> list = Db.find(sql,rec_id);
		  Iterator<Record> it=list.iterator();
		  while (it.hasNext()) {
		    Record qn = it.next();
			qn.set("url", ImageUploadKit.getImage(qn.getStr("id")));
			rellist.add(qn);
		}
		return rellist;
	}
	
	public int updateQnImage(String rel_id ,String id){
		
		String sql="update qiniu_image set rel_id =? where id=? ";
		return Db.update(sql, rel_id,id);
		
	}
	
	
	
	public int deleteQnImage(String id){
		ImageUploadKit.delete(id);
		String sql="delete from qiniu_image  where id=? ";
		return Db.update(sql,id);
	}
	
	public int getOrderId(){
		String sql ="select Max(order_id) mx  from qiniu_image ";
		QnImage qnImage = QnImage.dao.findFirst(sql);
		return qnImage.getInt("mx")+1;
	}
	
	
	public void bathUpdate(String rel_id ,String[] ids){
		for(int i =0 ;i<ids.length;i++ ){
			updateQnImage(rel_id,ids[i]);
		}
	}
	
	public void bathDetele(String[] ids){
		for(int i =0 ;i<ids.length;i++ ){
			deleteQnImage(ids[i]);
		}
	}

}
